<?php

namespace System;

//use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Configuration;

class DoctrineManager {

    public function createManager($cnf, $env) {
        if ($env == 'dev') {
            $cache = new \Doctrine\Common\Cache\ArrayCache;
        } else {
            $cache = new \Doctrine\Common\Cache\ArrayCache;
            //$cache = new \Doctrine\Common\Cache\ApcCache;
            //            $Memcached = new \Memcached();
//            
//            $Memcached->setOption(\Memcached::OPT_RECV_TIMEOUT, 1000);
//            $Memcached->setOption(\Memcached::OPT_SEND_TIMEOUT, 3000);
//            $Memcached->setOption(\Memcached::OPT_TCP_NODELAY, true);
//            $Memcached->setOption(\Memcached::OPT_PREFIX_KEY, $cnf['cacheKeyPrefix']);
//            $Memcached->addServer('localhost', 11211);
//            $cache = new \Doctrine\Common\Cache\MemcachedCache();
//            $cache->setMemcached($Memcached);
        }

        if (\Shared\Application::getInstance()->getDeleteCache()) {
            //$cache->deleteAll();
        }
        
        $config = new Configuration;
        $config->setMetadataCacheImpl($cache);
        $driverImpl = $config->newDefaultAnnotationDriver($cnf['paths']);
        $config->setMetadataDriverImpl($driverImpl);
        $config->setQueryCacheImpl($cache);
        $config->setProxyDir($cnf['proxyDir'] . DIRECTORY_SEPARATOR . $env);
        $config->setProxyNamespace($cnf['proxyNameSpace']);

        if ($env == 'dev') {
            $config->setAutoGenerateProxyClasses(true);
        } else {
            $config->setAutoGenerateProxyClasses(\Shared\Application::getInstance()->getDeleteCache());
        }

        $EntityManager = EntityManager::create($cnf['dbParams'], $config);

        return $EntityManager;
    }

}
